package com.real8.romemp.client.activity;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.real8.romemp.client.ClientFactory;
import com.real8.romemp.client.GameConstants;
import com.real8.romemp.client.place.ShipBuildingPlace;
import com.real8.romemp.client.place.ShipBuildingPlace.EMenuSelection;
import com.real8.romemp.client.place.TradeAllocationPlace;
import com.real8.romemp.client.ui.ShipBuildingView;
import com.real8.romemp.client.ui.ShipBuildingView.Presenter;
import com.real8.romemp.shared.GameData;

public class ShipBuildingActivity extends AbstractActivity implements Presenter {
	private final ClientFactory clientFactory;
	private EMenuSelection menuOption;

	// private int nShipsToBuild;

	public ShipBuildingActivity(ShipBuildingPlace place,
			ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
		this.menuOption = place.getMenuOption();
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		ShipBuildingView view = clientFactory.getShipBuildingView();
		if (view != null) {
			view.setPresenter(this);

			// prepare view
			view.hideAll();

			// enroll ship builders
			GameData gameData = clientFactory.getGameData();
			if (!gameData.getShipBuildersEnrolled()) {
				// enroll ship builders
				gameData.setUS_unallocatedShipBuilders(gameData
						.getLS_labourShipBuilders());
				gameData.setShipBuildersEnrolled(true);
			}

			// check still ships to build
			int nShipsToBuild = gameData.getUS_unallocatedShipBuilders();
			if (nShipsToBuild > 0) {
				// show menu
				int marinesWithoutShip = gameData.getLN_labourNavy()
						- gameData.getWS_warShips();
				int merchantsWithoutShip = gameData.getLM_labourMerchants()
						- gameData.getMS_merchantShips();
				view.showMenu(nShipsToBuild, marinesWithoutShip,
						merchantsWithoutShip);

				// menu selected? => show question
				switch (menuOption) {
				case WARSHIPS:
					view.showQuestion(menuOption);
					break;
				case MERCHANTSHIPS:
					view.showQuestion(menuOption);
					break;
				case DO_NOTHING:
					Scheduler.get().scheduleDeferred(new ScheduledCommand() {
						@Override
						public void execute() {
							goToNextPlace();
						}
					});
					break;
				}
			} else {
				// show allocated
				view.showNoMoreShipsToBuild();

				// delay and go to next step
				Timer timer = new Timer() {
					@Override
					public void run() {
						goToNextPlace();
					}
				};
				timer.schedule(GameConstants.DELAY_6010);
			}

			// set widget
			containerWidget.setWidget(view.asWidget());
		}
	}

	@Override
	public void onSelected(EMenuSelection selection) {
		goToMenu(selection);
	}

	private void goToMenu(EMenuSelection newMenuOption) {
		clientFactory.getPlaceController().goTo(
				new ShipBuildingPlace(newMenuOption));
	}

	private void goToNextPlace() {
		clientFactory.getPlaceController().goTo(new TradeAllocationPlace());
	}

	@Override
	public void onAnswer(int amount) {
		// check
		GameData gameData = clientFactory.getGameData();
		int nShipsToBuild = gameData.getUS_unallocatedShipBuilders();
		if (amount >= 0 && amount <= nShipsToBuild) {
			switch (menuOption) {
			case WARSHIPS:
				gameData.setUS_unallocatedShipBuilders(nShipsToBuild - amount);
				gameData.setWS_warShips(gameData.getWS_warShips() + amount);
				break;
			case MERCHANTSHIPS:
				gameData.setUS_unallocatedShipBuilders(nShipsToBuild - amount);
				gameData.setMS_merchantShips(gameData.getMS_merchantShips()
						+ amount);
				break;
			}

			// return to menu selection
			goToMenu(EMenuSelection.NONE);
		}
	}
}
